import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    currentUser: null,
    baseUrl: '/api'
  },
  mutations: {
    SET_CURRENT_USER: (state, payload) => {
      //刷新后state中的数据就没有了，所以对于当前用户数据来说可以考虑存到session中
      if (payload) {
        sessionStorage.setItem('currentUser',JSON.stringify(payload))
        state.currentUser = payload
      } else {
        state.currentUser = null
      }


      console.log('===',state.currentUser)
    }
  },
  getters: {
    getCurrentUser: (state) => {
      // if (!state.currentUser) {state.currentUser = JSON.parse(sessionStorage.getItem('currentUser'))}
      if (JSON.stringify(state.currentUser) === '{}') {
        state.currentUser = JSON.parse(sessionStorage.getItem('currentUser'))
      }
      console.log('---',state.currentUser)
      return state.currentUser !== null ? state.currentUser : '{}'
    }
  },
  actions: {
  },
  modules: {
  }
})
